﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace LiveWriterBackupWPF
{
    public class WLBSettings
    {
        public WLBSettings() { }

        public bool Settings { get; set; }

        public bool IncludeBlogTemplates { get; set; }

        public bool IncludeLinkGlossary { get; set; }

        public bool Plugins { get; set; }

        public bool Drafts { get; set; }

        public bool RecentPosts { get; set; }        

        public string WLBRegistryKey
        {
            get { return @"Software\ScottIsAFool\WindowsLiveWriterBackup\"; }
        }

        // Work Item #1895 http://wlwbackup.codeplex.com/WorkItem/View.aspx?WorkItemId=1895
        public bool AutoUpdate
        {
            get
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(WLBRegistryKey);
                if (key == null)
                {
                    return true;
                }
                else
                {
                    if (key.GetValue("AutoUpdate") == null)
                    {
                        return true;
                    }
                    else
                    {
                        return Convert.ToBoolean(key.GetValue("AutoUpdate"));
                    }
                }
            }
            set
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(WLBRegistryKey, true);
                if (key == null)
                {
                    key = Registry.CurrentUser.CreateSubKey(WLBRegistryKey);
                }
                key.SetValue("AutoUpdate", value);
                key.Close();
            }
        }

        public void Save()
        {
            Save(false);
        }

        public void Save(bool temp)
        {
            RegistryKey key;
            if (temp)
            {
                key = Registry.CurrentUser.OpenSubKey(WLBRegistryKey + @"Temp\", true);
            }
            else
            {
                key = Registry.CurrentUser.OpenSubKey(WLBRegistryKey, true);
            }
            try
            {
                if (key == null) // The key doesn't exist
                {
                    if (temp)
                    {
                        key = Registry.CurrentUser.CreateSubKey(WLBRegistryKey + @"Temp\");
                    }
                    else
                    {
                        key = Registry.CurrentUser.CreateSubKey(WLBRegistryKey);
                    }
                }
                key.SetValue("Settings", Settings);
                key.SetValue("IncludeBlogTemplates", IncludeBlogTemplates);
                key.SetValue("IncludeLinkGlossary", IncludeLinkGlossary);
                key.SetValue("Plugins", Plugins);
                key.SetValue("Drafts", Drafts);
                key.SetValue("RecentPosts", RecentPosts);
                key.Close();
            }
            catch (Exception ex)
            {
                WLBDebug.WriteToFile(ex.Message);
                WLBDebug.WriteToFile(ex.ToString());
            }
        }

        public void GrabSettings(bool temp)
        {
            RegistryKey key;
            if (temp)
            {
                key = Registry.CurrentUser.OpenSubKey(WLBRegistryKey + @"temp\");
            }
            else
            {
                key = Registry.CurrentUser.OpenSubKey(WLBRegistryKey);
            }
            try
            {
                if (key == null) // The key doesn't exist
                {
                    if (temp)
                    {
                        key = Registry.CurrentUser.CreateSubKey(WLBRegistryKey + @"temp\");
                    }
                    else
                    {
                        key = Registry.CurrentUser.CreateSubKey(WLBRegistryKey);
                    }
                    this.Drafts = true;
                    this.IncludeBlogTemplates = true;
                    this.IncludeLinkGlossary = true;
                    this.Plugins = true;
                    this.RecentPosts = false;
                    this.Settings = true;
                    this.Save();
                }
                else
                {
                    this.Drafts = Convert.ToBoolean(key.GetValue("Drafts"));
                    this.IncludeBlogTemplates = Convert.ToBoolean(key.GetValue("IncludeBlogTemplates"));
                    this.IncludeLinkGlossary = Convert.ToBoolean(key.GetValue("IncludeLinkGlossary"));
                    this.Plugins = Convert.ToBoolean(key.GetValue("Plugins"));
                    this.RecentPosts = Convert.ToBoolean(key.GetValue("RecentPosts"));
                    this.Settings = Convert.ToBoolean(key.GetValue("Settings"));
                }
                key.Close();
            }
            catch (Exception ex)
            {
                WLBDebug.WriteToFile(ex.Message);
                WLBDebug.WriteToFile(ex.ToString());
            }
        }

        public void GrabSettings()
        {
            GrabSettings(false);
        }
    }
}
